package com.hs.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.operation.OperationFeignClient;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageInfo;
import com.hs.car.pojo.Car;
import com.hs.car.pojo.vo.CarOrderVo;
import com.hs.car.pojo.vo.CarVo;
import com.hs.car.service.ICarService;
import com.hs.common.api.dto.PageBean;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.hs.order.pojo.Orders;
import com.hs.order.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆信息 前端控制器
 * </p>
 *
 * @author xudongZhou
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/car")
@Slf4j
@Api(tags = "车辆管理模块")
public class CarController {

    @Autowired
    private ICarService carService;

    @GetMapping("/lists")
    @ApiOperation("分页条件查询车辆")
    public ResultVo findCars(Integer currentPage, Integer pageSize, String licensePlate, String plateType, String nickName){

        PageInfo<CarVo> carVoPageInfo = carService.queryByPage(currentPage, pageSize, licensePlate, plateType, nickName);

        List<CarVo> records = carVoPageInfo.getList();

        if(!CollectionUtils.isEmpty(records)){
            ResultVo resultVo = new ResultVo();
            PageBean pageBean = new PageBean<>();
            pageBean.setList(records);
            pageBean.setTotals((int) carVoPageInfo.getTotal());

            resultVo.setData(pageBean);
            resultVo.setMsg("查询成功");

            return resultVo;


        }
        return new ResultVo(ResultCode.FAILED);

    }


    @PutMapping("/update")
    @ApiOperation("更改车辆状态或者解除绑定")
    public ResultVo changCarStatus(@RequestBody Car car){


        System.out.println(car);
        QueryWrapper qr = new QueryWrapper<>();
        qr.eq("license_plate",car.getLicensePlate());
        boolean b = carService.saveOrUpdate(car,qr);

        if(b){
            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @PostMapping("/add")
    public ResultVo addCar(@RequestBody Car car){


        boolean b = carService.saveOrUpdate(car);

        if(b){
            return new ResultVo(ResultCode.SUCCESS);
        }

        return new ResultVo(ResultCode.FAILED);
    }


}

